﻿using GameFramework;
using System;
using UnityEngine;
using XLua;

namespace GFTest
{
    /// <summary>
    /// 使用 Lua 脚本的 NGUIForm。
    /// </summary>
    [XLua.LuaCallCSharp]
    public class LuaNGUIForm : NGUIForm
    {
        [SerializeField]
        private string m_LuaScriptName = null;

        [SerializeField]
        private GameObject[] m_GameObjects = null;

        [SerializeField]
        private Transform[] m_Transforms = null;

        [SerializeField]
        private UIPanel[] m_UIPanels = null;

        [SerializeField]
        private UILabel[] m_UILabels = null;

        [SerializeField]
        private UIInput[] m_UIInputs = null;

        [SerializeField]
        private UIButton[] m_UIButtons = null;

        [SerializeField]
        private UIToggle[] m_UIToggles = null;

        [SerializeField]
        private UI2DSprite[] m_UI2DSprites = null;

        [SerializeField]
        private UI2DSpriteAnimation[] m_UI2DSpriteAnimations = null;

        [SerializeField]
        private UISprite[] m_UISprites = null;

        [SerializeField]
        private UISpriteAnimation[] m_UISpriteAnimations = null;

        [SerializeField]
        private UITexture[] m_UITextures = null;

        [SerializeField]
        private UISlider[] m_UISliders = null;

        [SerializeField]
        private UIProgressBar[] m_UIProgressBars = null;

        [SerializeField]
        private UIGrid[] m_UIGrids = null;

        [SerializeField]
        private UITable[] m_UITables = null;

        [SerializeField]
        private UIScrollView[] m_UIScrollViews = null;

        [SerializeField]
        private UIScrollBar[] m_UIScrollBars = null;

        [SerializeField]
        private UIPopupList[] m_PopupLists = null;

        [SerializeField]
        private LuaNGUIFormElement[] m_FormElements = null;

        [SerializeField]
        private UnityEngine.Object[] m_Objects = null;

        private LuaTable m_LuaTable = null;

        private Action<object> m_LuaOnInit = null;
        private Action<object> m_LuaOnOpen = null;
        private Action<object> m_LuaOnClose = null;
        private Action m_LuaOnPause = null;
        private Action m_LuaOnResume = null;
        private Action m_LuaOnCover = null;
        private Action m_LuaOnReveal = null;
        private Action<object> m_LuaOnRefocus = null;
        private Action<int, int> m_LuaOnDepthChanged = null;
        private Action<object> m_LuaOnEvent = null;
        private Action<GameObject> m_LuaOnClick = null;
        private Action<GameObject> m_LuaOnToggle = null;
        private Action<GameObject> m_LuaOnInputChange = null;
        private Action<GameObject> m_LuaOnPress = null;
        private Action<GameObject> m_LuaOnRelease = null;
        private Action<GameObject> m_LuaOnDragOver = null;
        private Action<GameObject> m_LuaOnDragOut = null;
        private Action m_LuaOnDestroy = null;

        public string LuaScriptName
        {
            get
            {
                return m_LuaScriptName;
            }
        }

        public GameObject[] GameObjects
        {
            get
            {
                return m_GameObjects;
            }
        }

        public Transform[] Transforms
        {
            get
            {
                return m_Transforms;
            }
        }

        public UIPanel[] UIPanels
        {
            get
            {
                return m_UIPanels;
            }
        }

        public UILabel[] UILabels
        {
            get
            {
                return m_UILabels;
            }
        }

        public UIInput[] UIInputs
        {
            get
            {
                return m_UIInputs;
            }
        }

        public UIButton[] UIButtons
        {
            get
            {
                return m_UIButtons;
            }
        }

        public UIToggle[] UIToggles
        {
            get
            {
                return m_UIToggles;
            }
        }

        public UI2DSprite[] UI2DSprites
        {
            get
            {
                return m_UI2DSprites;
            }
        }

        public UI2DSpriteAnimation[] UI2DSpriteAnimations
        {
            get
            {
                return m_UI2DSpriteAnimations;
            }
        }

        public UISprite[] UISprites
        {
            get
            {
                return m_UISprites;
            }
        }

        public UISpriteAnimation[] UISpriteAnimations
        {
            get
            {
                return m_UISpriteAnimations;
            }
        }

        public UITexture[] UITextures
        {
            get
            {
                return m_UITextures;
            }
        }

        public UISlider[] UISliders
        {
            get
            {
                return m_UISliders;
            }
        }

        public UIProgressBar[] UIProgressBars
        {
            get
            {
                return m_UIProgressBars;
            }
        }

        public UIGrid[] UIGrids
        {
            get
            {
                return m_UIGrids;
            }
        }

        public UITable[] UITables
        {
            get
            {
                return m_UITables;
            }
        }

        public UIScrollView[] UIScrollViews
        {
            get
            {
                return m_UIScrollViews;
            }
        }

        public UIScrollBar[] UIScrollBars
        {
            get
            {
                return m_UIScrollBars;
            }
        }

        public UIPopupList[] PopupLists
        {
            get
            {
                return m_PopupLists;
            }
        }

        public LuaNGUIFormElement[] FormElements
        {
            get
            {
                return m_FormElements;
            }
        }

        public UnityEngine.Object[] Objects
        {
            get
            {
                return m_Objects;
            }
        }

        public void OnEvent(object userData)
        {
            if (m_LuaOnEvent != null)
            {
                m_LuaOnEvent(userData);
            }
        }

        public void OnClick(GameObject gameObject)
        {
            if (m_LuaOnClick != null)
            {
                m_LuaOnClick(gameObject);
            }
        }

        public void OnToggle(GameObject gameObject)
        {
            if (m_LuaOnToggle != null)
            {
                m_LuaOnToggle(gameObject);
            }
        }

        public void OnInputChange(GameObject gameObject)
        {
            if (m_LuaOnInputChange != null)
            {
                m_LuaOnInputChange(gameObject);
            }
        }

        public void OnPress(GameObject gameObject)
        {
            if (m_LuaOnPress != null)
            {
                m_LuaOnPress(gameObject);
            }
        }

        public void OnRelease(GameObject gameObject)
        {
            if (m_LuaOnRelease != null)
            {
                m_LuaOnRelease(gameObject);
            }
        }

        public void OnDragOver(GameObject gameObject)
        {
            if (m_LuaOnDragOver != null)
            {
                m_LuaOnDragOver(gameObject);
            }
        }

        public void OnDragOut(GameObject gameObject)
        {
            if (m_LuaOnDragOut != null)
            {
                m_LuaOnDragOut(gameObject);
            }
        }

#if UNITY_2017_3_OR_NEWER

        protected override void OnInit(object userData)
#else

        protected internal override void OnInit(object userData)
#endif
        {
            base.OnInit(userData);

            if (string.IsNullOrEmpty(m_LuaScriptName))
            {
                Log.Error("Lua script name is invalid.");
                return;
            }

            m_LuaTable = GameEntry.Lua.NewTable();
            if (m_LuaTable == null)
            {
                Log.Error("Lua table is invalid.");
                return;
            }

            GameEntry.Lua.DoScript("UI/LuaNGUIFormFactory", m_LuaScriptName, m_LuaTable);
            m_LuaTable.Set("self", this);
            m_LuaTable.Get("OnInit", out m_LuaOnInit);
            m_LuaTable.Get("OnOpen", out m_LuaOnOpen);
            m_LuaTable.Get("OnClose", out m_LuaOnClose);
            m_LuaTable.Get("OnPause", out m_LuaOnPause);
            m_LuaTable.Get("OnResume", out m_LuaOnResume);
            m_LuaTable.Get("OnCover", out m_LuaOnCover);
            m_LuaTable.Get("OnReveal", out m_LuaOnReveal);
            m_LuaTable.Get("OnRefocus", out m_LuaOnRefocus);
            m_LuaTable.Get("OnDepthChanged", out m_LuaOnDepthChanged);
            m_LuaTable.Get("OnEvent", out m_LuaOnEvent);
            m_LuaTable.Get("OnClick", out m_LuaOnClick);
            m_LuaTable.Get("OnToggle", out m_LuaOnToggle);
            m_LuaTable.Get("OnInputChange", out m_LuaOnInputChange);
            m_LuaTable.Get("OnPress", out m_LuaOnPress);
            m_LuaTable.Get("OnRelease", out m_LuaOnRelease);
            m_LuaTable.Get("OnDragOver", out m_LuaOnDragOver);
            m_LuaTable.Get("OnDragOut", out m_LuaOnDragOut);
            m_LuaTable.Get("OnDestroy", out m_LuaOnDestroy);

            if (m_LuaOnInit != null)
            {
                m_LuaOnInit(userData);
            }
        }

#if UNITY_2017_3_OR_NEWER

        protected override void OnOpen(object userData)
#else

        protected internal override void OnOpen(object userData)
#endif
        {
            base.OnOpen(userData);

            if (m_LuaOnOpen != null)
            {
                m_LuaOnOpen(userData);
            }
        }

#if UNITY_2017_3_OR_NEWER

        protected override void OnClose(object userData)
#else

        protected internal override void OnClose(object userData)
#endif
        {
            if (m_LuaOnClose != null)
            {
                m_LuaOnClose(userData);
            }

            base.OnClose(userData);
        }

#if UNITY_2017_3_OR_NEWER

        protected override void OnPause()
#else

        protected internal override void OnPause()
#endif
        {
            base.OnPause();

            if (m_LuaOnPause != null)
            {
                m_LuaOnPause();
            }
        }

#if UNITY_2017_3_OR_NEWER

        protected override void OnResume()
#else

        protected internal override void OnResume()
#endif
        {
            base.OnResume();

            if (m_LuaOnResume != null)
            {
                m_LuaOnResume();
            }
        }

#if UNITY_2017_3_OR_NEWER

        protected override void OnCover()
#else

        protected internal override void OnCover()
#endif
        {
            base.OnCover();

            if (m_LuaOnCover != null)
            {
                m_LuaOnCover();
            }
        }

#if UNITY_2017_3_OR_NEWER

        protected override void OnReveal()
#else

        protected internal override void OnReveal()
#endif
        {
            base.OnReveal();

            if (m_LuaOnReveal != null)
            {
                m_LuaOnReveal();
            }
        }

#if UNITY_2017_3_OR_NEWER

        protected override void OnRefocus(object userData)
#else

        protected internal override void OnRefocus(object userData)
#endif
        {
            base.OnRefocus(userData);

            if (m_LuaOnRefocus != null)
            {
                m_LuaOnRefocus(userData);
            }
        }

#if UNITY_2017_3_OR_NEWER

        protected override void OnUpdate(float elapseSeconds, float realElapseSeconds)
#else

        protected internal override void OnUpdate(float elapseSeconds, float realElapseSeconds)
#endif
        {
            base.OnUpdate(elapseSeconds, realElapseSeconds);
            // 考虑到性能问题，OnUpdate 由 Lua 统一调用
        }

#if UNITY_2017_3_OR_NEWER

        protected override void OnDepthChanged(int uiGroupDepth, int depthInUIGroup)
#else

        protected internal override void OnDepthChanged(int uiGroupDepth, int depthInUIGroup)
#endif
        {
            base.OnDepthChanged(uiGroupDepth, depthInUIGroup);

            if (m_LuaOnDepthChanged != null)
            {
                m_LuaOnDepthChanged(uiGroupDepth, depthInUIGroup);
            }
        }

        private void OnDestroy()
        {
            if (m_LuaOnDestroy != null)
            {
                m_LuaOnDestroy();
            }

            m_LuaOnInit = null;
            m_LuaOnOpen = null;
            m_LuaOnClose = null;
            m_LuaOnPause = null;
            m_LuaOnResume = null;
            m_LuaOnCover = null;
            m_LuaOnReveal = null;
            m_LuaOnRefocus = null;
            m_LuaOnDepthChanged = null;
            m_LuaOnEvent = null;
            m_LuaOnClick = null;
            m_LuaOnToggle = null;
            m_LuaOnInputChange = null;
            m_LuaOnPress = null;
            m_LuaOnRelease = null;
            m_LuaOnDragOver = null;
            m_LuaOnDragOut = null;
            m_LuaOnDestroy = null;

            if (m_LuaTable != null)
            {
                m_LuaTable.Dispose();
                m_LuaTable = null;
            }
        }
    }
}
